﻿using ServiceStack;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethod.Redis
{
    public class RedisHelper
    {
        private static readonly string[] redisHosts = null;

        /// <summary>
        /// 端口
        /// </summary>
        private static readonly int rdisPort = System.Configuration.ConfigurationManager.AppSettings["RedisPort"].ToInt();

        /// <summary>
        /// 连接密码
        /// </summary>
        private static readonly string RdisPassword = System.Configuration.ConfigurationManager.AppSettings["RdisPassword"];

        /// <summary>
        /// redis连接地址
        /// </summary>
        public static string RedisHostStr = System.Configuration.ConfigurationManager.AppSettings["RedisHost"]; // "127.0.0.1"; //
        public static RedisClient client;

        static RedisHelper()
        {
            client = new RedisClient(RedisHostStr, rdisPort, RdisPassword, 1);
            //client = new RedisClient(RedisHostStr, rdisPort, null, 1);

            var s = client.ToJson();
        }

        /// <summary>
        /// 切换数据库
        /// </summary>
        /// <param name="db"></param>
        public static void ChangeDb(long db)
        {
            //切换到数据库
            client.ChangeDb(db);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expiry">固定到期时间</param>
        public static void Set<T>(string key, T value, DateTime expiry)
        {
            if (value == null)
            {
                return;
            }

            if (expiry <= DateTime.Now)
            {

                Remove(key);

                return;
            }
            try
            {
                if (client != null)
                {
                    client.SendTimeout = 1000;
                    client.Set(key, value, expiry - DateTime.Now);
                }
            }
            catch (Exception e)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "添加缓存", key);
            }
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        /// <param name="slidingExpiration">滑动到期时间</param>
        public static void Set<T>(string key, T value, TimeSpan slidingExpiration)
        {
            if (value == null)
            {
                return;
            }

            if (slidingExpiration.TotalSeconds <= 0)
            {
                Remove(key);
                return;
            }

            try
            {
                if (client != null)
                {
                    client.SendTimeout = 1000;
                    client.Set(key, value, slidingExpiration);
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return default(T);
            }
            try
            {
                if (client != null)
                {
                    client.SendTimeout = 1000;
                    return client.Get<T>(key);
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception e)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "添加缓存", key);
                return default(T);
            }
        }



        /// <summary>
        /// 移除指定redis缓存
        /// </summary>
        /// <param name="key">key</param>
        public static void Remove(string key)
        {
            try
            {
                if (client != null)
                {
                    client.SendTimeout = 1000;
                    client.Remove(key);
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key);
            }

        }

        /// <summary>
        /// 根据key判断是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Exists(string key)
        {
            try
            {
                if (client != null)
                {
                    client.SendTimeout = 1000;
                    return client.ContainsKey(key);
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key);
            }

            return false;
        }

    }
}
